package com.lyl.domaindemo.Infrastructure;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;

import java.io.Serializable;

/**
 * @author lyl
 * @date 2023/5/18
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResults<T> implements Serializable {

    private String code;

    private String massage;

    private T data;

    /**
     * 返回分页对象时的构造器
     * @param code
     * @param massage
     * @param page
     */
    public CommonResults(String code, String massage, Page<T> page){
        this.code  = code;
        this.massage = massage;
        this.data = (T)new PageImpl(
                page.getContent(),
                page.getNumber(),
                page.getSize(),
                page.getNumber(),
                page.getTotalPages());
    }

    public CommonResults(String code, String massage){
        this.code  = code;
        this.massage = massage;
    }

    public static<T> CommonResults<T> success(){
        CommonResults<T> results = new CommonResults<>();
        results.setCode(Constants.SUCCESS_CODE);
        results.setMassage(Constants.SUCCESS);
        return results;
    }

    public static<T> CommonResults<T> success(T data){
        CommonResults<T> results = new CommonResults<>();
        results.setCode(Constants.SUCCESS_CODE);
        results.setMassage(Constants.SUCCESS);
        results.setData(data);
        return results;
    }

    public static<T> CommonResults<T> failed(){
        CommonResults<T> results = new CommonResults<>();
        results.setCode(Constants.FAILED_CODE);
        results.setMassage(Constants.FAILED);
        return results;
    }
}
